package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Item.student;
import db.DBUtil;

public class StudentDao {
	static public void insert(student stu) throws SQLException{
		String sql="insert into student (studentnumber,password,realname,phone,email) values(?,?,?,?,?)";
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps=null;
		try{
			ps=conn.prepareStatement(sql);
			ps.setString(1, stu.getStudentnumber());
			ps.setString(2, stu.getPassword());
			ps.setString(3, stu.getRealname());
			ps.setString(4, stu.getPhone());
			ps.setString(5, stu.getEmail());
			ps.executeUpdate();
		}finally{
			conn.close();
		}
	}
	static public student getById(Integer id) throws SQLException{
		String sql="select * from student where id=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps=null;
		ResultSet rst=null;
		student stu=null;
		try{
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);
			rst=ps.executeQuery();
			if(rst.next()){
				stu=new student();
				stu.setId(rst.getInt(1));
				stu.setStudentnumber(rst.getString(2));
				stu.setPassword(rst.getString(3));
				stu.setRealname(rst.getString(4));
				stu.setPhone(rst.getString(5));
				stu.setEmail(rst.getString(6));
			}
		}finally{
			DBUtil.closeAll(null, conn, ps);
		}
		return stu;
	}
}
